package com.saop;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

public class Sodatabase {



    public String getSoDatabase(String sql, String... params) {
        //SELECT  ORDER_ITEM_ID   from order_item a where a.CUST_ORDER_ID = ?
        //SELECT provisale from  customer_order_group_rel a where a.CUST_ORDER_ID =?;
        Connection test_ora_so = null;
        PreparedStatement preparedStatement = null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");

                test_ora_so = DriverManager.getConnection("jdbc:mysql://136.192.50.109:60004/ORD_SO", "test_ora_so", "test@13579%!!so");

                preparedStatement = test_ora_so.prepareStatement(sql);

                for (int a = 1; a < params.length + 1; a++) {
                    preparedStatement.setString(a, params[a - 1]);
                }


                ResultSet resultSet = preparedStatement.executeQuery();
                StringBuffer s = new StringBuffer();

                while (resultSet.next()) {
                    //获取此条结果的所有字段
                    ResultSetMetaData metaData = resultSet.getMetaData();
                    int columnCount = metaData.getColumnCount();
                    for (int i = 1; i < columnCount + 1; i++) {
                        String columnName = metaData.getColumnName(i);
                        switch (columnName) {
                            case "ORDER_ITEM_ID":
                                s.append(resultSet.getString("ORDER_ITEM_ID")).append(",");
                                s.substring(0, s.length() - 1);
                                break;
                            case "PROVISALE":
                                s.append(resultSet.getString("PROVISALE"));
                                break;
                            case "CUST_ORDER_ID":
                                s.append(resultSet.getString("CUST_ORDER_ID"));
                                break;
                        }
                    }
                    //s.append(orderItemId + ",");
                }
                return s.toString().endsWith(",") ? s.substring(0, s.length() - 1) : s.toString();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                test_ora_so.close();
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
